package user_main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"log"
	"os"
	"pm_back_server/user-server/global"
	"pm_back_server/user-server/model"
	"time"
)

func main() {
	genTable()
}

// genTable /* 生成表
func genTable() {
	dsn := "root:mcr20010715@tcp(127.0.0.1:3306)/pm_server_test_db?charset=utf8&parseTime=True&loc=Local"

	newLogger := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
		SlowThreshold: time.Second,
		LogLevel:      logger.Info,
		Colorful:      true,
	})

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		panic(fmt.Sprintf("connect db faily, error: %s", err.Error()))
	} else {
		fmt.Println("connect db successfully")
	}

	_ = db.AutoMigrate(&model.User{})

	for i := 0; i < 10; i++ {
		user := model.User{
			Nickname: fmt.Sprintf("bobby%d", i),
			Mobile:   fmt.Sprintf("1878222222%d", i),
			Password: global.Encode("admin123"),
		}
		db.Save(&user)
	}
}
